package com.dalonedrow.engine.ui.impl.border;

import com.dalonedrow.engine.sprite.base.SpriteBase;
import com.dalonedrow.engine.sprite.impl.BasicLineSpriteObject;
import com.dalonedrow.engine.sprite.impl.BasicQuadSpriteObject;
import com.dalonedrow.engine.sprite.impl.GroupOfLines;
import com.dalonedrow.engine.systems.JOGLErrorHandler;
import com.dalonedrow.engine.systems.Render;
import com.dalonedrow.engine.ui.base.border.GuiBorder;
import com.dalonedrow.globalvariables.constants.hq.GlobalConstants;

/**
 * A simple border, similar to those found in the early 90's SNES games.
 * @author Donald
 */
public final class VXBorderNoEdge extends GuiBorder {
	/** the middle sprite. */
	private SpriteBase	middle;
	/** the middle sprite. */
	private GroupOfLines	lines;
	/** Creates a new instance of {@link VXBorderNoEdge}. */
	public VXBorderNoEdge() {
		final int three = 3, four = 4, eight = 8;
		super.setInsets(
				four,
				four,
				four,
				four
				);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		Render.getInstance().registerEntity(middle);
		Render.getInstance().registerEntity(lines);
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPosition(final double x, final double y, final double z) {
		super.setPosition(x, y, z);
		positionSprites();
	}
	/** Positions all sprites for the border. */
	private void positionSprites() {
		if (super.getPosition() != null) {
			lines.setPosition(
					super.getPosition().getX(), 
					super.getPosition().getY(), 
					super.getPosition().getZ()
			);
			lines.clearFloatStore();
			middle.setPosition(
					super.getPosition().getX(), 
					super.getPosition().getY(), 
					super.getPosition().getZ() 
					+ (GlobalConstants.CHILD_STEP * 0.1f)
			);
			middle.clearFloatStore();			
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void setPreferredSize(final int width, final int height) {
		super.setPreferredSize(width, height);
		try {
			final int three = 3, four = 4, eight = 8;
			if (middle == null) {
				{ // middle
					middle = new BasicQuadSpriteObject();
					middle.setRed(GlobalConstants.COL_SNES_BORDER[0]);
					middle.setGreen(GlobalConstants.COL_SNES_BORDER[1]);
					middle.setBlue(GlobalConstants.COL_SNES_BORDER[2]);
					middle.setAlpha(
							GlobalConstants.COL_SNES_BORDER[three]);
					middle.setModRed(GlobalConstants.COL_VX_NO_EDGE[0]);
					middle.setModGreen(GlobalConstants.COL_VX_NO_EDGE[1]);
					middle.setModBlue(GlobalConstants.COL_VX_NO_EDGE[2]);
					middle.setModAlpha(
							GlobalConstants.COL_VX_NO_EDGE[three]);
					middle.assignFlag(
							SpriteBase.FLAG_SPRITE_GRADIENT_VERTICAL_B2T);
				}
				{ // lines
					lines = new GroupOfLines();
					// start 1st line 2px below top
					float x = 0, y = height - 2;
					while (y > 2) {
						BasicLineSpriteObject l = new BasicLineSpriteObject();
						l.setAlpha(0.2f);
						l.setPosition(x, y, 0);
						l.setHeight(1);
						l.setWidth(width);
						lines.addImage(l);
						y -= 2;
					}
				}
			}
			// set middle position and width
			middle.setWidth(width);
			middle.setHeight(height);
			for (int i = 0; i < lines.getNumberOfImages(); i++) {
				lines.getImage(i).setWidth(width);
			}
			
			positionSprites();
		} catch (Exception ex) {
			JOGLErrorHandler.getInstance().fatalError(ex);
		}
	}
}
